<?php
require_once "includes/config.php";
require_once "includes/functions.php";
require_once 'includes/class.phpmailer.php';
require_once('AmazonAPI/.config.inc.php');
ob_start();
session_start();
$sessid=session_id();
$msg = "";

$service = new AmazonAPI_FPS_Client(AWS_ACCESS_KEY_ID, 
                                       AWS_SECRET_ACCESS_KEY);
$request =  new AmazonAPI_FPS_Model_PayRequest();


if(isset($_REQUEST['signature']) && !empty($_REQUEST['signature'])) {
	$tokenID = $_REQUEST['tokenID'];
	$callerReference = $_REQUEST['callerReference'];
	//print_r($_SESSION); exit;
									   
	$total_recipients = count($_SESSION['recipient_tokens']);
	$loop_limit = ($total_recipients - 1);
	for($i=0;$i<=$loop_limit;$i++){
		
		//echo $_SESSION['recipient_tokens'][$i]."<br />"; exit;
		$request->setCallerDescription('Swapen');
		$request->setCallerReference("swapen".rand());
		
		$request->setMarketplaceVariableFee('1.1');
		$request->setRecipientTokenId($_SESSION['recipient_tokens'][$i]);
		
		$request->setSenderTokenId($tokenID);//set the proper senderToken here.
		
		$amount = new AmazonAPI_FPS_Model_Amount();
		$amount->setCurrencyCode("USD");
		$amount->setValue($_SESSION['recipient_amounts'][$i]); //set the transaction amount here;
		
		$request->setTransactionAmount($amount);
	
		// object or array of parameters
	  	$return = invokePay($service, $request);
		//$return=true;
		
		
					
		
		if($return  && $return[2]!="Failure"){
			$user_id = $_SESSION['user_id_sess'];
			$item_arr = $_SESSION['item_arr'];
			$ref_comm_id = $item_arr[0];
			$qry =  mysql_query("update tbl_refer_comm set isRefunded=1 where ref_comm_id=$ref_comm_id") or die(mysql_error());
			
			
				
			$_SESSION['order_msg'] = "Your transaction is successfull";
			header("location:index.php?mySwapen");
		}
		else
		{
			header("location:index.php?mySwapen");
		}
	}
	 
	//use the above token ID to pay
}
function invokePay(AmazonAPI_FPS_Interface $service, $request) 
{
	try {
		$response = $service->pay($request);
			  //$res_arr = array();
		if($response){
		      $payResult = $response->getPayResult();
		      //$PayResult = $response->getPayResult();
		      $TransactionId = $payResult->getTransactionId();
		      $TransactionStatus = $payResult->getTransactionStatus();
		      $responseMetadata = $response->getResponseMetadata();
		      $RequestId = $responseMetadata->getRequestId();
		      $res_arr = array($PayResult,
						       $TransactionId,
						       $TransactionStatus,
						       $RequestId
		      );
		}
		return $res_arr;
              
                

	}
	catch (AmazonAPI_FPS_Exception $ex) {
		echo("Caught Exception: " . $ex->getMessage() . "\n");
		echo("Response Status Code: " . $ex->getStatusCode() . "\n");
		echo("Error Code: " . $ex->getErrorCode() . "\n");
		echo("Error Type: " . $ex->getErrorType() . "\n");
		echo("Request ID: " . $ex->getRequestId() . "\n");
		echo("XML: " . $ex->getXML() . "\n");
		return false;
	}
}
?>